package com.kaifamiao.kfm.controller;

import com.kaifamiao.kfm.entity.SysMenu;
import com.kaifamiao.kfm.entity.TreeMenuDTO;
import com.kaifamiao.kfm.service.SysMenuService;
import com.kaifamiao.kfm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @RequestMapping
    public ModelAndView menu(){
        return new ModelAndView("menu.html");
    }

    @GetMapping("/list")
    public JsonResult list(){
        List<SysMenu> list = sysMenuService.list();
        return JsonResult.success("success", list);
    }

    @GetMapping("/tree")
    public JsonResult treeList(){
        List<TreeMenuDTO> list = sysMenuService.treeMenu(sysMenuService.list());
        return JsonResult.success("success", list);
    }

    @RequestMapping("/1")
    public ModelAndView menu1(){
        return new ModelAndView("menu1.html");
    }


    @GetMapping("/add")
    public ModelAndView add(String pid, String id){
        ModelAndView modelAndView = new ModelAndView("add.html");
        if (StringUtils.hasLength(pid)){
            modelAndView.addObject("parent", new SysMenu());
        }
        return modelAndView;
    }
}
